/*
 * Copyright (C) 2011 Julia Mlinnik
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jmlinnik.android.mediascanner;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class MediaScannerActivity extends Activity {

    private IntentFilter mIntentFilter;
    private ViewFlipper mScanFlipper;
    private TextView mInfo;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mediascan_app);

        mScanFlipper = (ViewFlipper) findViewById(R.id.scan_flipper);
        mScanFlipper.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
                        .parse("file://"
                                + Environment.getExternalStorageDirectory())));

            }
        });

        mInfo = (TextView) findViewById(R.id.info);
        mInfo.setText(getResources().getString(R.string.press_me));

        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
        mIntentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
        mIntentFilter.addDataScheme("file");
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mBroadcastReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mBroadcastReceiver, mIntentFilter);
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED)) {
                runScan();
            } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
                stopScan();
            }

        }
    };

    private void runScan() {
        mScanFlipper.startFlipping();
        mInfo.setText(getResources().getString(R.string.scanning));
    }

    private void stopScan() {
        mScanFlipper.stopFlipping();
        mInfo.setText(getResources().getString(R.string.press_me));
    }
}